const http=require("http");
const routes=require("./module/routes")
const ejs=require("ejs")
http.createServer((req,res)=>{
    // 创建静态web服务
    routes.static(req,res,'static')
    // 路由
    let url=new URL(req.url,"http://localhost")
    pathname=url.pathname;
    if(pathname=='/login'){
        let msg="测试数据",list=[{title:"news",time:"2012-10-11"},{title:"news1",time:"2012-11-11"},];
        ejs.renderFile("./views/login.ejs", {msg,list}, function(err, data){
             // str => Rendered HTML string
             if(err){
                console.log(err)
                return
            }
            res.writeHead(200,{'Content-Type':"text/html;charset=UTF8"})
            res.end(data)
        });
    }else if(pathname=='/doLogin'){
        // 获取post参数
        let postData="";
        req.on("data",(data)=>{
            postData+=data
        })
        req.on("end",()=>{
            console.log(postData)
            res.writeHead(200,{'Content-Type':"text/html;charset=UTF8"})
            res.end(postData)
        })

       

    }else if(pathname=='/news'){
        console.log(req.method)//请求类型
        console.log(url.searchParams.get("page"))
        
    }else{
        res.writeHead(404,{'Content-Type':"text/html;charset=UTF8"})
        res.end("页面不存在")
    }
}).listen(3000)